package com.dao.impl;

import com.dao.FinancingDao;
import com.entry.Financing;
import com.entry.Instituty;
import com.pojo.FinanceList;
import com.pojo.FinancingAmout;
import com.pojo.FinancingAnd;
import com.util.DBHelper;
import com.util.PageHelper;

import java.util.ArrayList;

public class FinancingDaoImpl implements FinancingDao {
    private DBHelper db=new DBHelper();
    @Override
    public ArrayList<FinancingAmout> selectQuery(FinancingAnd financingAnd, PageHelper pageHelper) {
        StringBuilder strb = new StringBuilder("select financing.* , instituty.name, instituty.interest, instituty.receipt_day from financing INNER JOIN instituty on financing.instituty_id=instituty.instituty_id  where 1=1");
        ArrayList list = new ArrayList();
        if (financingAnd.getNo() != null) {
            strb.append(" and no like ?");
            list.add("%" + financingAnd.getNo() + "%");
        }
        if (financingAnd.getAmount() != null) {
            strb.append(" and amount = ?");
            list.add(financingAnd.getAmount());
        }
        if (financingAnd.getName() != null) {
            strb.append(" and name = ? ");
            list.add(financingAnd.getName());
        }
        if (financingAnd.getCreate_time() != null) {
            strb.append(" and create_time = ? ");
            list.add(financingAnd.getCreate_time());
        }
        if (financingAnd.getMinAmout() != null) {
            strb.append(" and ?<=amount ");
            list.add(financingAnd.getMinAmout());
        }
        if (financingAnd.getMaxAmout() != null) {
            strb.append(" and amount<=? ");
            list.add(financingAnd.getMaxAmout());
        }if (financingAnd.getStatus() != null) {
            strb.append(" and status = ?");
            list.add(financingAnd.getStatus());
        }
        strb.append(" limit ?,?");
        int startIndex=(pageHelper.getIndexPage()-1)*pageHelper.getShowData();
        list.add(startIndex);
        list.add(pageHelper.getShowData());
        return db.selectinfo(strb.toString(), FinancingAmout.class, list.toArray());
    }

    @Override
    public boolean insertFinancing(Financing financing) {
        String sql="insert into financing values(null,?,?,?,?,?,?,?,?,?,?,?,?,?) ";
        return db.update(sql,financing.getNo(),financing.getEnterprise_id(),financing.getAmount(),financing.getCost(),
                financing.getReceipt_amount(),financing.getInstituty_id(),financing.getCreate_time(),financing.getReceipt_time(),financing.getUplink_address(),
                financing.getStatus(),financing.getProgress_status(),financing.getPayment_interest_type(),financing.getRemark())!=0?true:false;
    }

    @Override
    public Financing selectByFinancing_id(int Financing_id) {
        return null;
    }

    @Override


    public FinanceList selectByInsitituty_id(int Financing_id) {
        String sql="SELECT\n" +
                "instituty.`name`,\n" +
                "financing.`no`,\n" +
                "financing.create_time,\n" +
                "financing.amount,\n" +
                "financing.`status`,\n" +
                "financing.payment_interest_type,\n" +
                "voucher.expiry_time,\n" +
                "financing.receipt_amount\n" +
                "FROM\n" +
                "instituty\n" +
                "INNER JOIN voucher ON voucher.instituty_id = instituty.instituty_id\n" +
                "INNER JOIN financing ON financing.instituty_id = voucher.instituty_id\n" +
                " where financing_id=?";
        return (FinanceList) db.selectinfo(sql,FinanceList.class,Financing_id).get(0);
    }

    @Override
    public int selectFinanciingSize(FinancingAnd financingAnd) {

        StringBuilder strb = new StringBuilder("select financing.* , instituty.name, instituty.interest, instituty.receipt_day from financing INNER JOIN instituty on financing.instituty_id=instituty.instituty_id  where 1=1");
        ArrayList list = new ArrayList();
        if (financingAnd.getNo() != null) {
            strb.append(" and no like ?");
            list.add("%" + financingAnd.getNo() + "%");
        }
        if (financingAnd.getAmount() != null) {
            strb.append(" and amount = ?");
            list.add(financingAnd.getAmount());
        }
        if (financingAnd.getName() != null) {
            strb.append(" and name = ? ");
            list.add(financingAnd.getName());
        }
        if (financingAnd.getCreate_time() != null) {
            strb.append(" and create_time = ? ");
            list.add(financingAnd.getCreate_time());
        }
        if (financingAnd.getMinAmout() != null) {
            strb.append(" and ?<=amount ");
            list.add(financingAnd.getMinAmout());
        }
        if (financingAnd.getMaxAmout() != null) {
            strb.append(" and amount<=? ");
            list.add(financingAnd.getMaxAmout());
        }if (financingAnd.getStatus() != null) {
            strb.append(" and status = ?");
            list.add(financingAnd.getStatus());
        }
        return db.selectinfo(strb.toString(), FinancingAmout.class, list.toArray()).size();
    }
}
